/**
 * @author yag
 * 文件名:2019.成都.DocumentationConfig.CLASS
 * 创建人:   yag
 * 创建时间: 2019/1/9 0009 14:11
 * 描述: swagger 文档处理 ||  DocumentationConfig
 * 历史信息:
 * <作者>          <更新时间>                    <版本>          <版本描述>
 * yag           2019/1/9 0009 14:11         v1.0.0.0              创建基础
 */
package com.example.zuul.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈一句话功能简述〉 类 DocumentationConfig <br> 
 * 〈swagger 文档处理〉
 *
 * @author yag
 * @create 2019/1/9 0009 14:11
 * @since 1.0.0
 */
@EnableSwagger2
@Component
@Primary
@Slf4j
public class DocumentationConfig implements SwaggerResourcesProvider {
    /**
     * discoveryClient 可用来获取 已注册的列表服务信息
     */
    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 获取 已注册的服务 并且 单独处理下不需要swagger API 文档管理的服务
     * oauth-center 认证服务
     * zuul 路由网关服务
     * discovery-server 注册中心服务
     * import-export-web 导入导出服务
     * @return
     */
    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        List<String> services =discoveryClient.getServices();
        for( String service : services ){
            log.info("获取已注册的服务services " + service);
            List<ServiceInstance> serviceInstances =  discoveryClient.getInstances(service);
            for(ServiceInstance route : serviceInstances){
                String serviceId=route.getServiceId().toLowerCase();
                if(serviceId.contains("oauth-center")
                    || serviceId.contains("zuul")
                    || serviceId.contains("discovery-server")
                    || serviceId.contains("import-export-web")
                    || serviceId.contains("fastdfs-file-manage")
                ){
                    continue;
                }
                resources. add(createResource(route.getServiceId(), serviceId, "2.0"));
                log.info("    services:" + service + ":getHost()=" + route.getHost());
                log.info("    services:" + service + ":getPort()=" + route.getPort());
                log.info("    services:" + service + ":getServiceId()=" + serviceId);
                log.info("    services:" + service + ":getUri()=" + route.getUri());
                log.info("    services:" + service + ":getMetadata()=" + route.getMetadata());
            }

        }
      /*  properties.getRoutes().values().stream()
                .forEach(route ->
                        resources
                                .add(createResource(route.getServiceId(), route.getServiceId(), "2.0")));*/
        return resources;
    }

  @Autowired
  ZuulProperties properties;

    /**
     * 组装 swagger api 服务相关信息
     * 若存在路由网关前缀，也需要补齐，如 properties.getPrefix() 就是获取网关前缀
     * @param name 服务名称
     * @param location 服务路径
     * @param version 版本
     * @return
     */
    private SwaggerResource createResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);

        swaggerResource.setLocation(properties.getPrefix()+"/" + location + "/v2/api-docs");
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }

}
